/// @tags:
#include <atcoder/modint>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;
using ll = long long;
using mint = atcoder::modint;

namespace BlueQuantum {

int main() {
  int n, m;
  cin >> n >> m;
  mint::set_mod(m);

  std::vector<mint> pow_n(n, 1);
  for (int i = 0; i < n - 1; i++) {
    pow_n[i + 1] = pow_n[i] * n;
  }

  mint ans = 0;
  mint perm = 1;
  for (int l = 1; l <= n; l++) {
    ans += perm * pow_n[n - l] * ((ll)l * (l - 1) / 2);
    perm *= n - l;
  }
  ans *= n;
  cout << ans.val() << endl;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("G - Only Once.in", "r", stdin);
  freopen("G - Only Once.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
